#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QFileDialog>

void MainWindow::changeItemCaption(QTreeWidgetItem *item)
{
    if (!item) {
        return;
    }
    QString str = "*" + item->text(colItem);
    item->setText(colItem, str);
    if (item->childCount() > 0) {
        for (int i = 0; i < item->childCount(); i++) {
            changeItemCaption(item->child(i));
        }
    }
}

void MainWindow::displayImage(QTreeWidgetItem *item)
{
    QString file_name = item->data(colItem, Qt::UserRole).toString();
    lbl_file_name->setText(file_name);
    lbl_node_text->setText(item->text(colItem));
    m_pixmap.load(file_name);
    // 调整图片大小
    ui->action_suitable_width->trigger();
    ui->action_suitable_height->trigger();
}

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    // 初始化 tw_file
    ui->tw_file->clear(); // 清空内容
    // 创建表头
    QTreeWidgetItem* headers = new QTreeWidgetItem();
    headers->setText(colItem, "目录和文件");
    headers->setText(colItemType, "节点类型");
    headers->setText(colItemDate, "最后的修改日期");
    // 设置表头的对齐方式
    headers->setTextAlignment(colItem, Qt::AlignHCenter | Qt::AlignVCenter);
    headers->setTextAlignment(colItemType, Qt::AlignHCenter | Qt::AlignVCenter);
    headers->setTextAlignment(colItemDate, Qt::AlignHCenter | Qt::AlignVCenter);
    ui->tw_file->setHeaderItem(headers);

    // 添加一个根节点
    QTreeWidgetItem* topItem = new QTreeWidgetItem(itTopItem);
    topItem->setText(colItem, "图片");
    topItem->setText(colItemType, "类型");
    topItem->setText(colItemDate, "最后修改时间");
    // 设置 Item 的一些行为(属性)
    topItem->setFlags(Qt::ItemIsSelectable | Qt::ItemIsUserCheckable | Qt::ItemIsEnabled | Qt::ItemIsAutoTristate);
    topItem->setCheckState(colItem, Qt::CheckState::Checked);
    ui->tw_file->addTopLevelItem(topItem);

    lbl_node_text = new QLabel("节点标题", this);
    lbl_node_text->setMinimumWidth(200);
    ui->statusbar->addWidget(lbl_node_text);

    spin_ratio = new QSpinBox(this);
    spin_ratio->setRange(0, 2000);
    spin_ratio->setValue(100);
    spin_ratio->setSuffix(" %");
    spin_ratio->setReadOnly(true);
    spin_ratio->setButtonSymbols(QAbstractSpinBox::NoButtons);
    ui->statusbar->addPermanentWidget(spin_ratio);

    lbl_file_name = new QLabel("文件名", this);
    ui->statusbar->addPermanentWidget(lbl_file_name);


}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_action_add_folder_triggered()
{
    // 弹出一个文件选择对话框
    QString dir_path = QFileDialog::getExistingDirectory();
    if (dir_path.isEmpty()) {
        return;
    }

    QTreeWidgetItem* parent_item = ui->tw_file->currentItem();
    if (!parent_item) {
        return;
    }
    if (parent_item->type() == itImageItem) {
        // 图片类型的 Item 不能放目录
        return;
    }

    // 获取目录名称
    int dir_len = dir_path.length();
    int index = dir_path.lastIndexOf("/");
    QString dir_name = dir_path.right(dir_len - index - 1);

    // 新建一个节点
    QTreeWidgetItem* item = new QTreeWidgetItem(itGroupItem);
    item->setText(colItem, dir_name);
    item->setText(colItemType, "Group Item");
    item->setFlags(Qt::ItemIsSelectable | Qt::ItemIsUserCheckable | Qt::ItemIsEnabled | Qt::ItemIsAutoTristate);
    item->setCheckState(colItem, Qt::CheckState::Checked);
    item->setData(colItem, Qt::UserRole, QVariant(dir_path));
    parent_item->addChild(item);
}


void MainWindow::on_action_add_file_triggered()
{
    QStringList file_names = QFileDialog::getOpenFileNames(this, "选择文件", "", "Images(*.jpg)");
    if (file_names.isEmpty()) {
        return;
    }

    QTreeWidgetItem* cur_item = ui->tw_file->currentItem();
    if (!cur_item) {
        cur_item = ui->tw_file->topLevelItem(0);
    }
    QTreeWidgetItem* parent_item = nullptr;
    if (cur_item->type() == itImageItem) {
        parent_item = cur_item->parent();
    } else {
        parent_item = cur_item;
    }

    for (int i = 0; i < file_names.size(); i++) {
        QString file_name = file_names.at(i);
        QFileInfo file_info(file_name);
        QDateTime date_time = file_info.lastModified();
        QTreeWidgetItem* item = new QTreeWidgetItem(itImageItem);
        QIcon icon(file_info.absoluteFilePath());
        item->setIcon(colItem, icon);
        item->setText(colItem, file_info.fileName());
        item->setText(colItemType, "image Item");
        item->setText(colItemDate, date_time.toString("yyyy/MM/dd"));
        item->setFlags(Qt::ItemIsSelectable | Qt::ItemIsUserCheckable | Qt::ItemIsEnabled | Qt::ItemIsAutoTristate);
        item->setCheckState(colItem, Qt::CheckState::Checked);
        item->setData(colItem, Qt::UserRole, QVariant(file_name));
        parent_item->addChild(item);
    }
    parent_item->setExpanded(true);
}


void MainWindow::on_action_delete_node_triggered()
{
    QTreeWidgetItem* cur_item = ui->tw_file->currentItem();
    if (!cur_item) {
        return;
    }
    QTreeWidgetItem* parent_item = cur_item->parent();
    parent_item->removeChild(cur_item);
}


void MainWindow::on_tw_file_currentItemChanged(QTreeWidgetItem *current, QTreeWidgetItem *previous)
{
    if (!current || current == previous) {
        return;
    }
    switch (current->type()) {
    case itTopItem :
    {
        ui->action_add_folder->setEnabled(true);
        ui->action_add_file->setEnabled(true);
        ui->action_delete_node->setEnabled(false);
        break;
    }
    case itImageItem :
    {
        ui->action_add_folder->setEnabled(false);
        ui->action_add_file->setEnabled(true);
        ui->action_delete_node->setEnabled(true);
        // 显示图片
        displayImage(current);
        break;
    }
    case itGroupItem :
    {
        ui->action_add_folder->setEnabled(true);
        ui->action_add_file->setEnabled(true);
        ui->action_delete_node->setEnabled(true);
        // 显示图片

        break;
    }

    }
}


void MainWindow::on_action_traverse_node_triggered()
{
    for (int i = 0; i < ui->tw_file->topLevelItemCount(); i++) {
        changeItemCaption(ui->tw_file->topLevelItem(i));
    }

}


void MainWindow::on_action_suitable_width_triggered()
{
    int w = ui->scrollArea->width() - 30;
    int realw = m_pixmap.width();
    m_ratio = (float)w / realw;

    spin_ratio->setValue(m_ratio * 100);
    QPixmap pix = m_pixmap.scaledToWidth(w - 30);
    ui->lbl_pic->setPixmap(pix);
}


void MainWindow::on_action_suitable_height_triggered()
{
    int w = ui->scrollArea->height() - 30;
    int realw = m_pixmap.height();
    m_ratio = (float)w / realw;

    spin_ratio->setValue(m_ratio * 100);
    QPixmap pix = m_pixmap.scaledToHeight(w - 30);
    ui->lbl_pic->setPixmap(pix);
}


void MainWindow::on_action_actual_size_triggered()
{
    // 直接加载原图即可
    ui->lbl_pic->setPixmap(m_pixmap);
    m_ratio = 1;
    spin_ratio->setValue(100);
}


void MainWindow::on_action_amplify_triggered()
{
    m_ratio *= 1.2;
    int w = m_ratio * m_pixmap.width();
    int h = m_ratio * m_pixmap.height();
    ui->lbl_pic->setPixmap(m_pixmap.scaled(w, h));
    spin_ratio->setValue(100 * m_ratio);
}


void MainWindow::on_action_reduce_triggered()
{
    m_ratio *= 0.8;
    int w = m_ratio * m_pixmap.width();
    int h = m_ratio * m_pixmap.height();
    ui->lbl_pic->setPixmap(m_pixmap.scaled(w, h));
    spin_ratio->setValue(100 * m_ratio);
}

